package com.baigt.j8.lambda;

import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;

/**
 * 方法引用 示例
 */
public class MethodReferenceDemo {
    public static void main(String[] args) {

        List<MethodReference> list= Arrays.asList(new MethodReference("baigt",75),new MethodReference("AiT",70),new MethodReference("Ricky",90),new MethodReference("Micky",80));
        // 类名::静态方法名
        list.sort(MethodReference::CompareMRByScore);
//        list.sort((m1,m2)->m1.getScore()-m2.getScore());
        list.forEach(r->{System.out.println(r.getScore());});
        System.out.println("#################");
        MethodReferenceCompare mrc=new MethodReferenceCompare();
        // 引用名::实例方法名
        list.sort(mrc::CompareByScore);
        list.forEach(r->{System.out.println(r.getScore());});
        System.out.println("#################");
        // 类名::实例方法
        list.sort(MethodReference::CompareByName);
        list.forEach(r->{System.out.println(r.getName());});

        System.out.println("#################");
        // 构造方法
        System.out.println(getString(String::new));
    }

    public static String getString(Supplier<String> supplier) {
        return supplier.get()+"New Create";
    }
}
class MethodReference{
    private String name;
    private Integer score;

    public MethodReference(String name, Integer score) {
        this.name = name;
        this.score = score;
    }
    public MethodReference(String name) {
        this.name = name;
        this.score = 0;
    }

    public MethodReference() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getScore() {
        return score;
    }

    public void setScore(Integer score) {
        this.score = score;
    }

    public static int CompareMRByScore(MethodReference m1,MethodReference m2){
        return m1.getScore()-m2.getScore();
    }

    public int CompareByName(MethodReference m1){
        System.out.println(this.getName()+"===="+m1.getName()+"--");
        return this.getName().compareToIgnoreCase(m1.getName());
    }

}
class MethodReferenceCompare{

    public int CompareByScore(MethodReference m1,MethodReference m2){
        return m2.getScore()-m1.getScore();
    }

}